package com.qf.controller;

import com.qf.dto.Page;
import com.qf.entity.Department;
import com.qf.entity.Employee;
import com.qf.service.DepartmentService;
import com.qf.service.EmpolyeeService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class EmployeeController {

    @Resource
    private EmpolyeeService empolyeeService;
    @Resource
    private DepartmentService departmentService;
    @GetMapping("/")
    public String index(){
        return "index";
    }

    //展示员工列表
    @GetMapping("/employeelist")
    public String employee_list(Model model){
        List<Employee> emps =empolyeeService.getALLemp();
        model.addAttribute("emps",emps);
        return "employee_list";
    }


    //根据id查询员工
    @GetMapping("/employee_update/{empId}")
    public String employee_update(@PathVariable Integer empId ,Model model){
        Employee empolyee = empolyeeService.findEmpolyeeById(empId);
        model.addAttribute("empolyee",empolyee);
        List<Department> departments = departmentService.getAllDepartment();
        model.addAttribute("departments",departments);
        return "employee_update";
    }


    //增加一个用户
    @GetMapping("/employee_add")
  public String employee_add(Model model){
        List<Department> departments = departmentService.getAllDepartment();
        model.addAttribute("departments",departments);
        return "employee_add";
  }
    @PostMapping("/saveAddEmpolyee")
  public String saveAddEmpolyee(Employee employee){
        empolyeeService.addEmpolyee(employee);
        return "redirect:/employeelist";
  }

//修改保存员工信息
@PostMapping("/saveEmloyee")
public String saveEmpolyee(Employee employee){
    System.out.println(employee+"=====****");
  empolyeeService.updateEmpolyee(employee);
    return "redirect:/employeelist";
}

//根据员工id删除一个员工
    @GetMapping("/deleteEmpolyee/{empId}")
    public String deleteEmpolyee(@PathVariable Integer empId){
        empolyeeService.deleteEmpolyee(empId);
        return "redirect:/employeelist";
    }


    /*
    * js获取分页数据
    * */
    @ResponseBody
    @GetMapping("/getEmployeePage")
    public Page<Employee> getEmployeePage(Integer pageNo,Integer pageSize){
        System.out.println(pageNo);
        System.out.println(pageSize);
//        if(pageNo==null){
//            pageNo=1;
//        }
//        if (pageSize==null){
//            pageSize=5;
//        }
        Page<Employee> page = empolyeeService.getEmployeePage(pageNo, pageSize);
        System.out.println(page);
        return page;
    }

    //添加一个员工
    public String addUser2(){
        return "user";
    }
}
